<?php

namespace App\Store\Models\Financial\Goods;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Basic\GoodsStyle;
use App\Store\Models\Goods\GoodsReplenishInfolist;
use App\Store\Models\Goods\GoodsStockList;

class GoodsWarning extends Model
{
    protected $table = 'site_financial_goods_warning';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表 
     */
    public static function getList(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['storeinfo',
        'goodscategoryinfo',
        'goodsstyleinfo'
        ])->where(['cp_id' => $cp_id, 'is_del' => 1])->orderBy('create_at', 'desc');
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }


        $is_gram=intval($request->is_gram);

        if(in_array($is_gram,['1','2'])){
            $query->whereHas('goodscategoryinfo', function ($query) use ($is_gram)  {
                $query->where('is_gram', $is_gram);
            });
        }
        

        $store_id = intval($request->store_id);//下拉单选搜索
        if ($store_id>0) {
            $query->where('store_id', $store_id);
        }
        // $goods_category_id = intval($request->goods_category_id);//下拉单选搜索
        // if ($goods_category_id>0) {
        //     $query->where('goods_category_id', $goods_category_id);
        // }

          $store_ids= $request->store_ids?? [];//下拉单选搜索

        if ($store_ids>0) {
            $query->whereIn('store_id', $store_ids);
        }


        $goods_category_ids=$request->goods_category_ids;
        $goods_category_ids=is_array($goods_category_ids)?$goods_category_ids:(is_array(json_decode($goods_category_ids,true))?json_decode($goods_category_ids,true):[]);
        if(count($goods_category_ids)>0){
            $query->whereIn('goods_category_id',$goods_category_ids);
        }


        $goods_style_id = intval($request->goods_style_id);//下拉单选搜索
        if ($goods_style_id>0) {
            $query->where('goods_style_id', $goods_style_id);
        }


        
        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }
        


        $isgetall = intval($request->isgetall) ?? 0;


        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            $list= Common::paginateDataToArray($list);

        } else {
            $list = $query->get()->toArray();
            $list=['list' => $list];
        }

        foreach($list['list'] as &$val){
            $val['warningdays']=$val['warning_at']>0?ceil((time()-$val['warning_at'])/86400):0;
            // //预警后补货件数
            // if($val['warningdays']>0){
            //     $val['replenish_num']=GoodsReplenishInfolist::where(['audit_status'=>2,'cp_id'=>$cp_id,'store_id'=>$val['store_id'],'goods_category_id'=>$val['goods_category_id'],'goods_style_id'=>$val['goods_style_id']])
            //     ->where([['create_at','>=',$val['warning_at']]])->sum('count');
            // }else{
            //     $val['replenish_num']=0;
            // }




            //当前库存
            $val['stock_num']=GoodsStockList::where(['cp_id'=>$cp_id,'store_id'=>$val['store_id'],'goods_category_id'=>$val['goods_category_id'],'goods_style_id'=>$val['goods_style_id'],'is_del'=>1])
                ->sum('count');


            $val['replenish_num']=($val['safety_stock_min']-$val['stock_num'])>0?($val['safety_stock_min']-$val['stock_num']):0;


        }


        //获取数据

        return $list;
    }

    public function storeinfo(){
        return $this->hasOne(SiteStore::class, 'id', 'store_id');
    }

    public function goodscategoryinfo(){
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id');
    }

    public function goodsstyleinfo(){
        return $this->hasOne(GoodsStyle::class, 'id', 'goods_style_id');
    }
}
